#include "svgbezieritem.h"
#include <QPainter>
#include <QPainterPath>
#include <QDebug>
#include <QLabel>
#include<QStyleOptionGraphicsItem>

// 通过四个点（起点S1、终点E1、两控制点S2、E2）初始化贝塞尔曲线
SvgBezierItem::SvgBezierItem(QPointF start, QPointF end, int Width , QColor color, QBrush brush_input)
    :CShape(start, end, Width, color, brush_input)
{
}
SvgBezierItem::SvgBezierItem(qreal start_x,qreal start_y, qreal end_x, qreal end_y,int Width, QColor color, QBrush brush_input )
    :SvgBezierItem(QPointF(start_x, start_y),QPointF(end_x, end_y), Width, color, brush_input)
{
}

void SvgBezierItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)

{
    Q_UNUSED(widget);
    QPainterPath path;
    painter->setPen(pen());
    path.moveTo(points[S1]);   //起始点
    // 添加贝塞尔曲线绘制路径
    path.cubicTo(points[S2], points[E2], points[E1]);
    // 设置抗锯齿
    painter->setRenderHint(QPainter::Antialiasing, true);
    // 绘制贝塞尔曲线
    painter->drawPath(path);
    QPen nPen;
    nPen.setWidth(1);
    nPen.setColor("black");
    painter->setPen(nPen);
    // 被选中时，绘制4个控制点及其连线
    if(option->state & QStyle::State_Selected) {
        // 绘制4个控制点
        for (int i= 0; i < 4; i++ ) {
            if(i != WitchPointsisSelected) painter->drawRect(PointsforDraw[i]);
            else {
                painter->setBrush(Qt::red);
                painter->drawRect(PointsforDraw[i]);
                painter->setBrush(Qt::NoBrush);
            }
        }
        // 绘制连线
        // 保存当前painter状态
        //设置连线样式
        nPen.setStyle(Qt::DotLine);
        nPen.setColor(QColor(200, 100, 100));

        painter->setPen(nPen);
        // 绘制
        painter->drawLine(points[S1], points[S2]);
        painter->drawLine(points[E1], points[E2]);
        painter->drawLine(points[S2], points[E2]);
    }

}

QPainterPath SvgBezierItem::shape() const {
    // 创建贝塞尔曲线的路径
    QPainterPath path;
    path.moveTo(points[S1]);
    path.cubicTo(points[S2], points[E2], points[E1]);
    // 创建一个 QPainterPathStroker 对象，并设置其宽度为当前画笔的宽度
    QPainterPathStroker stroker;
    stroker.setWidth(pen().widthF()+15);

    // 使用 QPainterPathStroker对象对路径进行描边，并得到描边后的路径
    QPainterPath strokerPath = stroker.createStroke(path);
    // 将控制点加入路径中以保证其能被选中
    for (int i =0 ; i < 4; i++) strokerPath.addEllipse(PointsforDraw[i]);
    // 返回最终的描边路径
    return strokerPath;
}

QRectF SvgBezierItem::boundingRect() const{
    return this->shape().boundingRect();
}
void SvgBezierItem::mousePressEvent(QGraphicsSceneMouseEvent* event)
{
    // 初始化为未选中点也未进入旋转状态
    WitchPointsisSelected = -1;
    isRotating = false;
    // 检查鼠标左键和Shift键是否同时按下
    if (event->buttons() & Qt::LeftButton && event->modifiers() & Qt::ShiftModifier) {
        // 将正在旋转标志设为true
        isRotating = true;
        // 记录鼠标按下时的场景坐标
        BeginScenePos = event->scenePos();
        // 记录图形项的初始变换矩阵
        BeginTransform = transform();
    }
    else {
        setCursor(Qt::CursorShape::SizeAllCursor);
        // 遍历4个绘制点的区域
        for (int i = 0; i < 4 ; i++) {
            // 检查鼠标位置是否在绘制点的区域内
            if (PointsforDraw[i].contains(event->pos())) {
                // 将被选中的点索引保存
                WitchPointsisSelected = i;
                setSelected(true); // 设置图形项为选中状态以保证控制点和边界框的显示
                break;
            }
        }
        // 检查是否有控制点被选中
        if (WitchPointsisSelected != -1) {
            // 将被选中点的位置更新到鼠标位置实现吸附效果
            points[WitchPointsisSelected] = event->pos();
            GenerateControlPoints();
            prepareGeometryChange();
            update();
        }
        else QGraphicsItem::mousePressEvent(event);
    }
}



void SvgBezierItem::mouseMoveEvent(QGraphicsSceneMouseEvent* event)
{
    if (WitchPointsisSelected != -1) {
        // 移动选中的控制点
        setSelected(true);
        points[WitchPointsisSelected] = event->pos();
        GenerateControlPoints();
        prepareGeometryChange();
        update();
    }
    else if (isRotating && (event->buttons() & Qt::LeftButton && event->modifiers() & Qt::ShiftModifier)) {
        setSelected(true);
        SupportRotate(event->scenePos(), mapToScene(boundingRect().center())); // 支持旋转操作
    }
    else QGraphicsItem::mouseMoveEvent(event);
}

void SvgBezierItem::mouseReleaseEvent(QGraphicsSceneMouseEvent* event)
{
    setCursor(Qt::CursorShape::ArrowCursor);
    if(WitchPointsisSelected != -1) {
        WitchPointsisSelected = -1;
        update();
    }
    QGraphicsItem::mouseReleaseEvent(event);
}












